package xin.zhiyin.控制.system.系统;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xin.zhiyin.实体.系统菜单Entity;
import xin.zhiyin.工具.JthinkTree;
import xin.zhiyin.工具.ResponseBo;
import xin.zhiyin.工具.TreeUtils;
import xin.zhiyin.服务.系统菜单Service;

import java.util.*;

@Controller
//@RestController
@RequestMapping(path = "/system/config/menu")//页面的url地址
public class 菜单Controller {
    @Autowired
    private 系统菜单Service 菜单Service;

    @RequestMapping("/systemMenuTreeList")
    @ResponseBody
    public ResponseBo getSystemMenuTreeList() {
        List<JthinkTree<系统菜单Entity>> tree = new ArrayList<>();
        List<系统菜单Entity> terms = 菜单Service.获取所有系统菜单();
        if (null != terms) {
            buildTrees(tree, terms);
            JthinkTree<系统菜单Entity> termTree = TreeUtils.buildTree(tree);
            List<JthinkTree<系统菜单Entity>> nodes=new ArrayList<>();
            nodes.add(termTree);
            return ResponseBo.ok(nodes);
        }
        return ResponseBo.ok();
    }
    private void buildTrees(List<JthinkTree<系统菜单Entity>> tree, List<系统菜单Entity> terms) {
        terms.forEach(term -> {
            JthinkTree<系统菜单Entity> treeNode = new JthinkTree<>();
            treeNode.setTitle(term.getName());
            Long parentId = term.getParentId();
            treeNode.setId(term.getId().toString());
            if (null != parentId) {
                treeNode.setParentId(parentId.toString());
            }
            tree.add(treeNode);
        });
    }
    @GetMapping(path = "/list")
    public String 后台首页(){
        return "system/config/menu/list";
    }
    @PostMapping("/add")
    @ResponseBody
    public ResponseBo addSystemMenu(系统菜单Entity 菜单) {
        if(StringUtils.isBlank(菜单.getName())) {
            return ResponseBo.error("请输入系统菜单名称");
        }
        菜单Service.保存(菜单);
        return ResponseBo.ok("新增系统菜单成功");
    }
    @PostMapping("/update")
    @ResponseBody
    public ResponseBo updateSystemMenu(系统菜单Entity 菜单) {
        菜单Service.更新(菜单);
        return ResponseBo.ok("更新系统菜单成功");
    }
    @RequestMapping("/listData")
    @ResponseBody
    public String getAllSystemMenus(String termName) {

        系统菜单Entity 菜单Entity = new 系统菜单Entity();
        Iterator<系统菜单Entity> 菜单Iter = 菜单Service.获取所有系统菜单().iterator();
        List<LinkedHashMap<String, Object>> 菜单List = new ArrayList<>();
        while(菜单Iter.hasNext())
        {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            菜单Entity = 菜单Iter.next();
            map.put("id",菜单Entity.getId());
            map.put("parentId",菜单Entity.getParentId());
            map.put("name",菜单Entity.getName());
            map.put("link",菜单Entity.getLink());
            map.put("icon",菜单Entity.getIcon());
            map.put("status",菜单Entity.getStatus());
            map.put("sort",菜单Entity.getSort());
            菜单List.add(map);
        }
        String josn菜单List = JSON.toJSONString(菜单List);
//        model.addAttribute("systemMenuList",josn菜单List);
        return josn菜单List;
    }
    @RequestMapping("/getSystemMenu")
    @ResponseBody
    public ResponseBo getSystemMenu(Long menuId) {
        系统菜单Entity systemMenu = 菜单Service.根据Id获取系统菜单(menuId);
        return ResponseBo.ok(systemMenu);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseBo deleteCategory(String termIds) {
        List<String> ids = Arrays.asList(termIds.split(","));
        List<Long> LString = new ArrayList();
        for(String st:ids)
        {
            LString.add(Long.valueOf(st));
        }
        菜单Service.批量删除(LString);

        return ResponseBo.ok(LString);
    }
}
